package com.xindaoapp.happypet.activity;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.text.Editable;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.ForegroundColorSpan;
import android.text.style.ImageSpan;
import android.util.AttributeSet;
import android.widget.EditText;

import com.xindaoapp.happypet.R;
import com.xindaoapp.happypet.utils.MergeBitmap;

public class MyEditText extends EditText {
    private final Context context;
    private SpannableString ss;
    private ImageSpan span;

    public MyEditText(Context context) {
        super(context);
        this.context = context;
    }

    public MyEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
    }

    public void insertDrawable(Drawable d2, String name, Editable editable, int selection) {

        MergeBitmap mergeBitmap = new MergeBitmap();
        ss = new SpannableString("\n" + name + "\n");
        // 得到drawable对象，即所有插入的图片
        // Drawable d2 = getResources().getDrawable(id);
        Drawable d1 = getResources().getDrawable(R.drawable.book_bg);
        // d2.setBounds(0, 0, d1.getIntrinsicWidth(), d1.getIntrinsicHeight());
        Bitmap bitmap1 = mergeBitmap.drawableToBitmap(d1);
        Bitmap bitmap2 = mergeBitmap.drawableToBitmap(d2);
        Bitmap bitmap = mergeBitmap.mergeBitmap(bitmap1, bitmap2);
        span = new ImageSpan(bitmap, ImageSpan.ALIGN_BASELINE);

        // 包括0但是不包括"easy".length()即：4。[0,4)
        ss.setSpan(span, "\n".length(), name.length() + 1, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
        editable.insert(selection, ss);
    }

    /**
     * 插入视频
     * 
     * @param id
     * @param name
     * @param editable
     * @param selection
     */
    public void insertVideo(int id, String name, Editable editable, int selection) {

        MergeBitmap mergeBitmap = new MergeBitmap();
        ss = new SpannableString("\n" + name + "\n");
        // 得到drawable对象，即所有插入的图片
        Drawable d2 = getResources().getDrawable(id);
        Drawable d1 = getResources().getDrawable(R.drawable.book_bg);
        // d2.setBounds(0, 0, d1.getIntrinsicWidth(), d1.getIntrinsicHeight());
        Bitmap bitmap1 = mergeBitmap.drawableToBitmap(d1);
        Bitmap bitmap2 = mergeBitmap.drawableToBitmap(d2);
        Bitmap bitmap = mergeBitmap.mergeBitmap(bitmap1, bitmap2);
        span = new ImageSpan(bitmap, ImageSpan.ALIGN_BASELINE);

        // 包括0但是不包括"easy".length()即：4。[0,4)
        ss.setSpan(span, "\n".length(), name.length() + 1, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
        editable.insert(selection, ss);
    }

    /**
     * 插入表情
     * 
     * @param id 表情R文件ID
     * @param name 插入的替换文字
     * @param editable 插入的控件
     * @param selection 插入的位置
     */
    public void insertFace(int id, String name, Editable editable, int selection) {

        ss = new SpannableString(name);
        // 得到drawable对象，即所有插入的图片
        Drawable d = getResources().getDrawable(id);
        d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
        span = new ImageSpan(d, ImageSpan.ALIGN_BASELINE);
        ss.setSpan(span, 0, name.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
        editable.insert(selection, ss);
    }

    /**
     * 图片后加一空格
     */
    public void addForeColorSpan(Editable editable, int selection) {
        SpannableString spanString = new SpannableString(" ");
        ForegroundColorSpan span = new ForegroundColorSpan(Color.TRANSPARENT);
        spanString.setSpan(span, 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        editable.insert(selection, spanString);
    }

}
